const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();

Page({
  data: {
    weeklist: ['日', '一', '二', '三', '四', '五', '六'],
    curr: '',
    end: '',
    pickdate: '',
    shownotice: false,
    theme: app.themeData,
  },

  onLoad(options) {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    if (month < 10) {
      month = '0' + month;
    }
    let end = year + '-' + month;
    this.setData({
      curr: end,
      end,
      year,
      month,
      day,
    });
    this.loadSetting();
    this.loadCurrentMonth(year, month);
  },

  getDaysWeekady(year, month) {
    let date = new Date(year, month - 1, 1);
    let date2 = new Date(year, month, 1);
    let days = new Date(date2.getTime() - 864e5).getDate();
    let weekday = date.getDay();
    this.setData({
      before: weekday,
      days,
    });
  },

  pickDate(e) {
    let str = e.detail.value;
    this.setData({
      pickdate: str,
    });
    this.loadCurrentMonth(str.split('-')[0], str.split('-')[1]);
  },

  signIn(e) {
    let that = this;
    let date = e.currentTarget.dataset.date;
    let { year, month, day } = that.data;
    if (
      this.getTime(year, month, day) ===
      this.getTime(date.split('-')[0], date.split('-')[1], date.split('-')[2])
    ) {
      app.ws_micro_shoprequest({
        url: api.default.signin_submit,
        data: {
          today: date,
        },
        success: function(res) {
          if (res.code === 0) {
            wx.showModal({
              title: '提示',
              content: '签到成功',
              showCancel: false,
              success: function() {
                that.loadSetting();
                that.loadCurrentMonth(date.split('-')[0], date.split('-')[1]);
              },
            });
          } else {
            wx.showToast({
              title: res.msg || '签到失败',
              icon: 'none',
              duration: 2000,
            });
          }
        },
      });
    } else {
      wx.showModal({
        title: '提示',
        content: '签到时间还未到',
        showCancel: false,
      });
    }
  },

  getTime(y, m, d) {
    return new Date(y, m, d).getTime();
  },

  loadSetting() {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.default.signsetting,
      success: function(res) {
        if (res.code === 0) {
          let { setting, continuation, today, total } = res.data;
          that.setData({
            setting,
            continuation,
            today,
            total,
          });
        }
      },
    });
  },

  loadCurrentMonth(y, m) {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    app.ws_micro_shoprequest({
      url: api.default.signin,
      data: {
        year: y,
        month: m,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          let { year, month, registerTime } = res.data;
          that.getDaysWeekady(year, month);
          that.setData({
            registerTime,
          });
        } else {
          that.getDaysWeekady(y, m);
        }
      },
      fail: function() {
        that.getDaysWeekady(y, m);
      },
    });
  },

  showNotice() {
    this.setData({
      shownotice: true,
    });
  },

  hideNotice() {
    this.setData({
      shownotice: false,
    });
  },
});
